package com.shuyu.web.controller.api;

import com.shuyu.common.core.domain.Result;
import com.shuyu.common.utils.DateUtils;
import com.shuyu.common.utils.StringUtils;
import com.shuyu.framework.web.service.CustomerLoginService;
import com.shuyu.vo.CustomerVo;
import com.shuyu.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 授权登录相关
 * 
 * @author
 */
@RestController
@RequestMapping("/api/authLogin")
@Api(tags = "前端-用户登录")
public class AuthLoginController
{
    private static final Logger log = LoggerFactory.getLogger(AuthLoginController.class);
    @Resource
    private CustomerLoginService customerLoginService;
    @Value("${token.expireTime}")
    private Integer expireTime;

    /**
     * 获取openId
     */
    @PostMapping("/login")
    @ApiOperation("微信快捷登录")
    public Result<TokenVo> login(@RequestBody CustomerVo customer) throws WxErrorException {
        log.info("/login?customer={}",customer);
        // 生成令牌
        String token = customerLoginService.customerLogin(customer);
        TokenVo tokenVo = new TokenVo(token,DateUtils.addMinutes(new Date(), expireTime));
        return Result.success("操作成功",tokenVo);
    }

    @GetMapping("/test")
    @ApiOperation("获取测试token")
    public Result<TokenVo> test(String userName, String password) throws WxErrorException {
        if(StringUtils.isEmpty(userName)||StringUtils.isEmpty(password)){
            return Result.error("登录失败");
        }
        if(userName.equals("test")&&password.equals("test123")){
            // 生成令牌
            String openId = "oNnLNwAImF8DmXobD1I-joP4coGo";
            String token = customerLoginService.testToken(openId);
            TokenVo tokenVo = new TokenVo(token,DateUtils.addMinutes(new Date(), expireTime));
            return Result.success("操作成功",tokenVo);
        }else {
            return Result.error("账号/密码错误");
        }
    }

}
